#!/usr/bin/perl

use strict;
 
use DBI;
use CGI;
use TTraker;

my $form_ref = parse_input();
print_header();
my $dbh;
initialize_dbi();

my %FORM_DATA = ();
%FORM_DATA = %$form_ref; 
my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $weekNum = $FORM_DATA{'weekNum'};

my ($key, $id_str, $pay_cat, $date); 

my %updates= ();
foreach $key (keys %FORM_DATA) {
  if ($key =~ /^payroll_data_/ ) {
 	 if ( $FORM_DATA{$key} eq "") { 
 	   $FORM_DATA{$key} = 0.0;
 	 }  
    $id_str = $key;
    $id_str =~ s/payroll_data_//;
    ($pay_cat,$date) = split /_/, $id_str;
    my $hrs = $FORM_DATA{$key};
    if ( not exists $updates{$date} ) {		
	    $updates{$date}=1;
	 }
#	  print "KEY: $key - $FORM_DATA{$key}<br/>"
  } else {   
#  print "KEY: $key - $FORM_DATA{$key}<br/>";   
  } 
}
my @Kes = keys %updates;
my @kes = sort @Kes;
foreach $key (@kes) {
   my $reg = $FORM_DATA{"payroll_data_reg_$key"};
   my $ot1 = $FORM_DATA{"payroll_data_ot1_$key"};
   my $ot2 = $FORM_DATA{"payroll_data_ot2_$key"};
   my $vac = $FORM_DATA{"payroll_data_vac_$key"};
   my $sta = $FORM_DATA{"payroll_data_sta_$key"};
   my $sic = $FORM_DATA{"payroll_data_sic_$key"};
   my $brv = $FORM_DATA{"payroll_data_brv_$key"};
   my $abs = $FORM_DATA{"payroll_data_abs_$key"};
   my $loa = $FORM_DATA{"payroll_data_loa_$key"};
   my $hrs = $reg + $ot1 + $ot2 + $vac + $sta + $sic + $brv;
#	print "Updating $key $weekNum VALUES: $num, $hrs, $reg, $ot1, $ot2, $vac, $sta, $sic, $brv, $abs, $loa, THE DATE: $key, THE END <br>";
	adjust_daily_payroll_hours ( $num, $hrs, $reg, $ot1, $ot2, $vac, $sta, $sic, $brv, $abs, $loa, $key);
		
		
}     

trak_04_close_window();

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub trak_04_close_window
{
print <<ENDHEADER;
<html>
<head>
<link rel="stylesheet" type="text/css" href="${css_ref}trak.css" />
</head>
<script type="text/javascript"
	language="JavaScript">
	window.close();
</script>
</body>
</html>
ENDHEADER
}








